package com.wxl.common.wiget

import android.app.Activity
import android.content.Context
import android.text.TextUtils
import android.view.KeyEvent
import androidx.lifecycle.Observer
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.interfaces.OnCancelListener
import com.lxj.xpopup.interfaces.OnConfirmListener
import com.wxl.common.R
import com.wxl.common.base.AbsActivity
import com.wxl.common.bean.ProgressPopBean
import com.wxl.common.life.LifecycleManager
import kotlinx.android.synthetic.main.pop_cjq_up_layout.view.*

class ProgressUpdatePop : CenterPopupView {

    private var showBack: Runnable? = null

    private var isLock = false
    private var blockDialogShow = false
    private var mProgressPopBean:ProgressPopBean?=null

    constructor(context: Context) : super(context)

    override fun getImplLayoutId(): Int {
        return R.layout.pop_cjq_up_layout
    }

    override fun onCreate() {
        super.onCreate()
        val progressPopBean = ProgressPopBean()
        progressPopBean.observe((context as AbsActivity), Observer {
            isLock = it.isLock
            if (!blockDialogShow) {
                update(it)
            } else {
                mProgressPopBean = it
            }
        })
        LifecycleManager.manager.registerLiveData(
            progressPopBean,
            (context as AbsActivity).lifecycle
        )
    }


    private fun update(it:ProgressPopBean){
        if (!TextUtils.isEmpty(it.title)) {
            updateTitleProgress(it)
        } else {
            updateProgress(it.progress, it.sub)
        }
    }

    override fun onDismiss() {
        super.onDismiss()
        blockDialogShow = false
        LifecycleManager.manager.unRegisterLiveData(ProgressPopBean::javaClass.name)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK && !isLock) {
            showBlockDialog()
            return true
        }
        return super.onKeyDown(keyCode, event)
    }


    private fun showBlockDialog() {
        blockDialogShow = true
        XPopup.Builder(context)
            .dismissOnTouchOutside(false)
            .dismissOnBackPressed(false)
            .asCustom(CancelPop.asConfirm(context,"提示", "是否终止当前操作？", object : OnConfirmListener {
                override fun onConfirm() {
                    this@ProgressUpdatePop.dismiss()
                    if (context is Activity) {
                        (context as Activity).finish()
                    }
                }
            }, object : OnCancelListener {
                override fun onCancel() {
                    blockDialogShow = false
                    if(mProgressPopBean != null) {
                        update(mProgressPopBean!!)
                    }
                }

            }))
            .show()
    }


    private fun updateTitleProgress(pp: ProgressPopBean) {
        cjqUpdateProgressBar.max = pp.max
        if (pp.progress > 0) {
            cjqUpdateProgressBar.progress = pp.progress
        }
        cjqUpdateProgressBarTitle.text = pp.title
        if (!TextUtils.isEmpty(pp.sub)) {
            cjqUpdateProgressSub.text = pp.sub
        }

        cjqUpdateProgressText.text =
            "${(cjqUpdateProgressBar.progress.toFloat() * 100 / pp.max).toInt()}%"
        if (pp.progress < 0 || pp.progress >= pp.max) {
            delayDismiss(2000)
            return
        }
    }


    /**
     * 更新进度
     */
    private fun updateProgress(p: Int, sub: String) {
        if (p > 0) {
            cjqUpdateProgressBar.progress = p * 100 / 150
        }
        if (!TextUtils.isEmpty(sub)) {
            cjqUpdateProgressSub.text = sub
        } else {
            cjqUpdateProgressSub.text = "正在升级起爆器,请勿进行其他操作..."
        }
        cjqUpdateProgressText.text = "${cjqUpdateProgressBar.progress}%"

        if (p < 0 || p == 150) {
            delayDismiss(3000)
            return
        }
    }

    override fun doAfterShow() {
        super.doAfterShow()
        showBack?.run()
    }

    override fun doAfterDismiss() {
        super.doAfterDismiss()
        LifecycleManager.manager.unRegisterLiveData(ProgressPopBean::javaClass.name)
    }

    companion object {

        fun show(context: Context): ProgressUpdatePop {
            val pop = ProgressUpdatePop(context)
            XPopup.Builder(context)
                .dismissOnTouchOutside(false)
                .dismissOnBackPressed(false)
                .asCustom(pop)
                .show()
            (context as AbsActivity).addPop(pop)
            return pop
        }


        fun show(context: Context, showBack: Runnable): ProgressUpdatePop {
            val pop = ProgressUpdatePop(context)
            pop.showBack = showBack
            XPopup.Builder(context)
                .dismissOnTouchOutside(false)
                .dismissOnBackPressed(false)
                .asCustom(pop)
                .show()
            (context as AbsActivity).addPop(pop)
            return pop
        }

        fun updateProgress(p: Int, sub: String) {
            val progressPopBean = ProgressPopBean()
            progressPopBean.progress = p
            progressPopBean.sub = sub
            progressPopBean.title = "采集器升级"
            progressPopBean.max = 100
            LifecycleManager.manager.refreshLiveData(progressPopBean)
        }

        fun updateProgress(pop: ProgressUpdatePop, p: Int, sub: String, title: String, max: Int) {
            val progressPopBean = ProgressPopBean()
            progressPopBean.progress = p
            progressPopBean.sub = sub
            progressPopBean.title = title
            progressPopBean.max = max
            LifecycleManager.manager.refreshLiveData(progressPopBean)
        }

        fun updateProgress(p: Int, sub: String, title: String, max: Int) {
            val progressPopBean = ProgressPopBean()
            progressPopBean.progress = p
            progressPopBean.sub = sub
            progressPopBean.title = title
            progressPopBean.max = max
            LifecycleManager.manager.postLiveData(progressPopBean)
        }

        fun updateBlockLockProgress(p: Int, sub: String, title: String, max: Int) {
            val progressPopBean = ProgressPopBean()
            progressPopBean.progress = p
            progressPopBean.sub = sub
            progressPopBean.title = title
            progressPopBean.max = max
            progressPopBean.isLock = true
            LifecycleManager.manager.postLiveData(progressPopBean)
        }
    }
}